//游戏敌人类
class Enemy {
    private targer;//保留上级容器对象
    public body:egret.Bitmap;//主体
    public gold:egret.Bitmap;//主体
    public bloodBar:egret.Bitmap;//血条
    public bloodBackground:egret.Bitmap;//血条背景
    private name:string = "Enemy1";//名字
    private siteX:number = 480;//当前中心点位置
    private siteY:number = 0;//当前中心点位置
    private blood:number = 0;//剩余血量
    private canMove:boolean = true;//标示是否能继续前进
    private isDie:boolean = false;//标示是否已经死亡
    private isDieComp:boolean = false;//标示死亡特效是否完成

    private speed:number;//当前速度
    private iceTime:number = 0;//冰冻效果的计时器
    private windTime:number = 0;//暴风效果的计时器

    //构造函数//初始化数据
    constructor(target) {
        this.targer = target;
        this.init();
    }

    //初始化函数，对特有属性赋值
    public init():void {
        this.name = "Enemy" + Math.floor(Math.random() * GameData.enemyTypeNum + 1); //设置随机敌人
        this.gold = Tool.addBitmap(this.targer, "gold_png", 520, 90, 30, 30, false);
        this.body = Tool.addBitmap(this.targer, "role_" + this.name + "_png", 0, 0, 0, 0, false);
        this.bloodBackground = Tool.addBitmap(this.targer, "bloodBarB_png", 500, 120, 0, 0, false);
        this.bloodBar = Tool.addBitmap(this.targer, "bloodBar_png", 503, 123, 0, 0, false);
        this.blood = GameData[this.name].blood;
        this.speed = GameData[this.name].speed;
        this.body.x = this.siteX + GameData[this.name].offsetX;
        this.body.y = this.siteY + GameData[this.name].offsetY;
        this.body.scaleX = GameData[this.name].scale;
        this.body.scaleY = GameData[this.name].scale;
        this.gold.visible = false;
    }

    //死亡后的处理
    public die():void {
        this.isDie = true;
        this.gold.x = this.siteX + 40;

        if (Math.random() < GameData.goldChance) {
            GameData.goldNum++;
            this.gold.visible = true;
            var tw = egret.Tween.get(this.gold);
            tw.to({alpha: 0.3, y: 50}, 480);
        }

        var tw = egret.Tween.get(this.bloodBar);
        tw.to({width: 0}, 200).call(function () {
            this.targer.removeChild(this.bloodBar);
            this.targer.removeChild(this.bloodBackground);
        }, this);

        var tw = egret.Tween.get(this.body);
        tw.to({alpha: 0}, 500).call(function () {
            this.targer.removeChild(this.gold);
            this.targer.removeChild(this.body);
            this.body = null;
            this.isDieComp = true;
        }, this);
    }

    //刷新状态的函数
    public flash():void {
        if (this.isDie)return;

        if (this.iceTime > 1)this.iceTime--;//冰霜攻击效果
        else {
            this.iceTime = 0;
            this.speed = GameData[this.name].speed;
            this.body.texture = RES.getRes("role_" + this.name + "_png");
        }

        if (this.windTime > 1) {//暴风攻击效果
            this.windTime--;
            this.siteX += GameData.Attack.wind + GameData.elemNum * GameData.Attack.windUp;
            if (this.siteX > 480)this.windTime = 0;
            this.body.x = this.siteX + GameData[this.name].offsetX;
            this.bloodBar.x = 23 + this.siteX;
            this.bloodBackground.x = 20 + this.siteX;
            return;
        }
        else {
            this.windTime = 0;
        }

        if (GameData.attackType != "empty" && GameData.attackType != "blood") {
            if (GameData.attackType == "ice") {
                this.body.texture = RES.getRes("role_" + this.name + "_ice_png");
                this.blood -= GameData.Attack.ice + GameData.elemNum * GameData.Attack.iceUp;
                this.speed = GameData[this.name].speed / 2;
                this.iceTime = GameData.iceTime;
            }
            else if (GameData.attackType == "fire") {
                this.blood -= GameData.Attack.fire + GameData.elemNum * GameData.Attack.fireUp;
            }
            else if (GameData.attackType == "wind") {
                this.blood -= ( GameData.Attack.wind + GameData.elemNum * GameData.Attack.windUp) / 4 * 10;
                this.windTime = GameData.windTime;
            }
            var tempWidth = 56 * (this.blood / GameData[this.name].blood);
            var tw = egret.Tween.get(this.bloodBar);
            tw.to({width: tempWidth}, 200);
            console.log("123");

            if (this.blood < 1)this.die();
            else if (GameData.attackType != "die");//判断死亡和别攻击
            GameData.attackType = "empty";
        }
        else if (this.siteX > 110 && this.isDie == false) {
            if (this.canMove == true) {
                this.siteX -= this.speed;
                this.bloodBar.x = 23 + this.siteX;
                this.bloodBackground.x = 20 + this.siteX;
                this.body.x = this.siteX + GameData[this.name].offsetX;
            }
        }
        else if (this.siteX < 111) GameData.heroHit = true;


    }
}